interface NodeCmp<T> {
    int cmp(T d1, T d2);
}

class Node<T> {
    T data;
    Node<T> next;
    Node<T> prev;

    public Node() {
        data = null;
        next = null;
        prev = null;
    }

    public Node(T data, Node<T> next, Node<T> prev) {
        this.data = data;
        this.next = next;
        this.prev = prev;
    }
}

public class MList<T> {
    Node<T> head;
    int len;
    Node<T> it;

    public MList() {
        head = new Node<T>();
        head.next = head;
        head.prev = head;
        len = 0;
        it = null;
    }

    public MList add(T data) {
        Node<T> newNode = new Node<T>(data, head, head.prev);
        head.prev.next = newNode;
        head.prev = newNode;
        len++;
        return this;
    }

    public T find(T key, NodeCmp<T> cmpObj) {
        for (Node<T> tail = head.next; tail != head; tail = tail.next) {
            if (cmpObj.cmp(key, tail.data) == 0)
                return tail.data;
        }
        return null;
    } 
    public boolean next() {
        if (it == null) {
            it = head.next;
        } else if (it.next == head) {
            it = null;
            return false;
        } else {
            it = it.next;
        }
        return true;
    }

    public T value() {
        if (it == null)
            return null;
        return it.data;
    }

    public MList show() {
        System.out.printf("[");
        for (Node<T> tail = head.next; tail != head; tail = tail.next) {
            System.out.print(tail.data);
            if (tail.next != head)
                System.out.printf(", ");
        }
        System.out.printf("]\n");

        return this;
    }
}
